package cn.jsprun.utils;

import java.io.CharArrayWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class FileCaptureResponseWrapper extends HttpServletResponseWrapper {
	 private CharArrayWriter output;  
	 	public String toString(){  
	 		return output.toString();  
	 	}  
	    public FileCaptureResponseWrapper(HttpServletResponse response){  
	       super(response);  
	       output = new CharArrayWriter();  
	    }  
	    public PrintWriter getWriter(){  
	       return new PrintWriter(output);   
	    }  
	    public void writeFile(String fileName,String content)throws IOException{  
	      OutputStream out= new FileOutputStream(fileName);
	      OutputStreamWriter ot=new OutputStreamWriter(out,JspRunConfig.CHARSET);
	      ot.write(output.toCharArray());
	      if(content!=null){
	    	  ot.write(content);
	      }
	      ot.close();
	      out.close();
	    }  
	    public void writeResponse(PrintWriter out) {  
	      out.print( output.toCharArray() );  
	    }  
}
